package pt.isec.a21210392.a21190220.ludotwist;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Locale;

import android.R.string;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SetProfileActivity extends Activity
{
	EditText setNameTextField;
	File myFile;
		
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_set_profile);
		
		setNameTextField = (EditText) findViewById(R.id.SetProfileTextField);
		
		myFile = new File("/sdcard/ludotwistprofile.txt");
		if (myFile.exists())
		{
			try
			{
				FileInputStream fis = new FileInputStream(myFile);
				InputStreamReader isr = new InputStreamReader(fis);
				BufferedReader br = new BufferedReader(isr);
				setNameTextField.setText(br.readLine());
				br.close();
				isr.close();
				fis.close();
			}
			catch(Exception e){Log.d("A ler ficheiro...",e.getMessage().toString());}			
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.set_profile, menu);
		return true;
	}
	public void onSetProfileSave(View v)
	{
		try 
		{					
			myFile.createNewFile();
			
			FileOutputStream fos = new FileOutputStream(myFile);
			OutputStreamWriter osw = new OutputStreamWriter(fos);
			osw.write(setNameTextField.getText().toString());
			osw.close();
			fos.close();
			
			Toast.makeText(v.getContext(), getResources().getString(R.string.settings_saved),Toast.LENGTH_SHORT).show();
			SetProfileActivity.this.finish();
		} 
		catch(Exception e) 
		{
			Toast.makeText(v.getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
		}
	}
	public void onSetProfileBack(View v) 
	{
		this.finish();
	}
	public void onPTClick(View v)
	{
		String nick = setNameTextField.getText().toString();
		Log.d("nick:",nick);
		Locale locale = new Locale("pt");
		locale.setDefault(locale);
		Configuration config = new Configuration();
		config.locale = locale;
		getResources().updateConfiguration(config, null);
		setContentView(R.layout.activity_set_profile);
		setNameTextField = (EditText) findViewById(R.id.SetProfileTextField);
		setNameTextField.setText(nick);
	}
	public void onENClick(View v)
	{
		String nick = setNameTextField.getText().toString();
		Locale locale = new Locale("en");
		locale.setDefault(locale);
		Configuration config = new Configuration();
		config.locale = locale;
		getResources().updateConfiguration(config, null);
		setContentView(R.layout.activity_set_profile);
		setNameTextField = (EditText) findViewById(R.id.SetProfileTextField);
		setNameTextField.setText(nick);
	}
}
